package com.ihd.ihardware.view.enter.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ActivityForgetBinding;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.view.enter.viewModel.ResetViewModel;

import cn.wowjoy.commonlibrary.app.Constans;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

public class ForgetActivity extends BaseActivity<ActivityForgetBinding, ResetViewModel> {

    private CountDownTimer timer;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("忘记密码");
        binding.mtitlebar.setLeftBack(this);

        binding.phoneET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0 && binding.codeET.getText().length() > 0)
                    binding.nextTV.setEnabled(true);
                else
                    binding.nextTV.setEnabled(false);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        binding.codeET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0 && binding.phoneET.getText().length() > 0)
                    binding.nextTV.setEnabled(true);
                else
                    binding.nextTV.setEnabled(false);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        setRx(Constans.CODES, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
                DialogUtils.waitingDialog(ForgetActivity.this);
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                DialogUtils.dismiss(ForgetActivity.this);
                timer = new CountDownTimer(60 * 1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        binding.getCode.setEnabled(false);
                        binding.getCode.setText(millisUntilFinished / 1000 + "s");
                    }

                    @Override
                    public void onFinish() {
                        binding.getCode.setEnabled(true);
                        binding.getCode.setText("重新获取");
                    }
                }.start();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(ForgetActivity.this, error.getMessage());
                DialogUtils.dismiss(ForgetActivity.this);
            }
        });

        binding.getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.phoneET.getText().length() == 0)
                    ToastUtils.showShort(ForgetActivity.this, "请输入手机号");
                else
                    viewModel.sendVerifySms(binding.phoneET.getText().toString(), "3");
            }
        });

        binding.nextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(ForgetActivity.this, ResetActivity.class);
                i.putExtra("phone", binding.phoneET.getText().toString());
                i.putExtra("code", binding.codeET.getText().toString());
                startActivity(i);
                finish();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget;
    }

    @Override
    protected Class getViewModelClass() {
        return ResetViewModel.class;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null)
            timer.cancel();
    }
}
